package com.linhh.mall.center.server.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.OrderInfoService;
import com.linhh.mall.center.common.constant.ConstantKey;
import com.linhh.mall.center.common.dto.ChartDataDto;
import com.linhh.mall.center.common.dto.OrderCount;
import com.linhh.mall.center.common.dto.OrderCountData;
import com.linhh.mall.center.common.dto.OrderTimeDataDto;
import com.linhh.mall.center.common.pojo.*;
import com.linhh.mall.center.common.util.DateTimeUtil;
import com.linhh.mall.center.server.mappers.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @Author linhh
 * @Date 2023 03 04 16 09
 **/
@Service
public class OrderServiceImpl implements OrderInfoService {

    @Autowired
    private SubOrderInfoMapper subOrderInfoMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;


    @Override
    public PageInfo<SubOrderInfo> queryAll(Long shopId, Integer orderStatus, Integer page, Integer size) {
        PageHelper.startPage(page, size);

        Example example = new Example(SubOrderInfo.class);
        if (shopId != null && shopId > 0) {
            example.createCriteria().andEqualTo("shopId", shopId);
        }
        if (orderStatus >= 0) {
            example.createCriteria().andEqualTo("orderStatus", orderStatus);
        }
        List<SubOrderInfo> subOrderInfos = subOrderInfoMapper.selectByExample(example);
        PageInfo<SubOrderInfo> pageInfo = new PageInfo<>(subOrderInfos);
        return pageInfo;
    }

    @Override
    public List<OrderItem> getDetailInfo(Long orderSubId) {
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("subOrderId", orderSubId);
        List<OrderItem> tbOrderItems = orderItemMapper.selectByExample(example);
        return tbOrderItems;
    }

    @Override
    public OrderDelivery getDeliveryInfo(Long orderSubId) {
        Example example = new Example(OrderDelivery.class);
        example.createCriteria().andEqualTo("subOrderId", orderSubId);
        OrderDelivery tbOrderDeliverys =  orderDeliveryMapper.selectOneByExample(example);
        return tbOrderDeliverys;
    }

    @Override
    public List<SubOrderInfo> selectAll() {

        return subOrderInfoMapper.selectAll();
    }

    @Override
    public OrderTimeDataDto orderTimeDate(Long shopId) {
        int today = DateTimeUtil.dataToTimestampT(DateUtil.beginOfDay(new Date()));
        int tomorrow = DateTimeUtil.dataToTimestampT(DateUtil.endOfDay(new Date()));
        int yesterday = DateTimeUtil.dataToTimestampT(DateUtil.beginOfDay(DateUtil.yesterday()));
        int lastWeek = DateTimeUtil.dataToTimestampT(DateUtil.beginOfDay(DateUtil.lastWeek()));
        int nowMonth = DateTimeUtil.dataToTimestampT(DateUtil.beginOfMonth(new Date()));

        OrderTimeDataDto orderTimeDataDto = new OrderTimeDataDto();

        // 今日订单数
        orderTimeDataDto.setTodayCount(subOrderInfoMapper.countBetweenTime(shopId, today, tomorrow));
        // 昨日订单数
        orderTimeDataDto.setProCount(subOrderInfoMapper.countBetweenTime(shopId, yesterday, today));
        // 本周订单数
        orderTimeDataDto.setLastWeekCount(subOrderInfoMapper.countBetweenTime(shopId, lastWeek, tomorrow));
        // 本月订单数
        orderTimeDataDto.setMonthCount(subOrderInfoMapper.countBetweenTime(shopId, nowMonth, tomorrow));

        Example example = new Example(MemberInfo.class);
        orderTimeDataDto.setUserCount(memberInfoMapper.selectCountByExample(example));

        Example example1 = new Example(SubOrderInfo.class);
        example1.selectProperties("orderId").setDistinct(true);
        if(shopId > 0){
            example1.createCriteria().andEqualTo("shopId",shopId);
        }

        int count = subOrderInfoMapper.selectCountByExample(example1);

        orderTimeDataDto.setOrderCount(count);
        orderTimeDataDto.setPriceCount(subOrderInfoMapper.sumTotalPrice(shopId));
        Example example2 = new Example(GoodsInfo.class);
        orderTimeDataDto.setGoodsCount(goodsInfoMapper.selectCountByExample(example2));
        return orderTimeDataDto;
    }

    @Override
    public OrderCount getOrderCount(Long shopId) {
        OrderCount orderCount = new OrderCount();
        List<String> colums = new ArrayList<>();
        List<OrderCountData> orderCountDataList = subOrderInfoMapper.selectOrderStatus(shopId);
        for (OrderCountData count:orderCountDataList){
            if(ConstantKey.UNPAID.equals(count.getName())){
                count.setName("未支付");
            }else if(ConstantKey.INVALID_ORDER.equals(count.getName())){
                count.setName("订单失败");
            }else if(ConstantKey.SEAL_THE_DEAL.equals(count.getName())){
                count.setName("完成交易");
            }else if(ConstantKey.MERCHANDISE_RETURN.equals(count.getName())){
                count.setName("商品退货");
            }
            colums.add(count.getName());
        }
        orderCount.setColumn(colums);
        orderCount.setOrderCountDatas(orderCountDataList);
        return orderCount;
    }

    @Override
    public Map<String, Object> chartCount(Long shopId) {
        Map<String, Object> map = new LinkedHashMap<>();
        int start = DateTimeUtil.dataToTimestampT(DateUtil.beginOfMonth(new Date()));
        int end = DateTimeUtil.dataToTimestampT(DateUtil.endOfMonth(new Date()));
        List<ChartDataDto> chartDataDtos = subOrderInfoMapper.charList(shopId,start,end);
        List<ChartDataDto> chartTDataDtos = subOrderInfoMapper.charListT(shopId,start,end);
        map.put("chart",chartDataDtos);
        map.put("chartT",chartTDataDtos);
        return map;
    }
}
